package com.itfenghuang.day21;

import java.sql.*;

public class A02_使用java操作数据库 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        插入数据();
//        查询();
//        使用预编译对象查询();
        Class.forName("com.mysql.jdbc.Driver");
        Connection root = DriverManager.getConnection
                ("jdbc:mysql://127.0.0.1:3306/fh_day03",
                        "root", "123456");
        String name="大王八";
        String mima="456";
        int id=1;
//        通过id修改账号和密码
//        写sql语句
        String sql="update user set username=?,psaawrod=? where id=?";
//        获取预编译对象
        PreparedStatement pst = root.prepareStatement(sql);
//        给占位符赋值
        pst.setString(1,name);
        pst.setString(2,mima);
        pst.setInt(3,id);
        int i = pst.executeUpdate();
        root.close();
    }

    private static void 使用预编译对象查询() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection root = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3306/fh_day03",
                "root", "123456");

        //查询账号是 小乌龟的人
        String name="小乌龟";
        //?:占位符  条件先占位置
        String sql="select*from user where username=?";
//        获取预编对象  预先编译sql语句
        PreparedStatement pst = root.prepareStatement(sql);
//        给占位符赋值
//        parameterIndex 条件可能不止一个   指定是那个占位符   从1开始,有几个?到结束
//        x:要赋的值
//        void setString(int paeameterIdex, String,x)throws SQLExceptio
        pst.setString(1,name);
//        执行sql语句
        ResultSet rs = pst.executeQuery();
//        处理结果集
        while (rs.next()){
            String us = rs.getString("username");
            String pw = rs.getString("psaawrod");
            System.out.println(us+"="+pw);
        }
    }

    private static void 查询() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection root = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3306/fh_day03",
                "root", "123456");
        Statement st = root.createStatement();
        String sql = "select * from user where username='小乌龟'";
        ResultSet rs = st.executeQuery(sql);
        while (rs.next()){
            String us = rs.getString("username");
            String pw = rs.getString("psaawrod");
            System.out.println(us+"="+pw);
        }
    }

    private static void 插入数据() throws ClassNotFoundException, SQLException {
        //        加载驱动
        Class.forName("com.mysql.jdbc.Driver");
//        获取连接
        Connection conn = DriverManager.getConnection
                ("jdbc:mysql://127.0.0.1:3306/fh_day03",
                "root", "123456");
//        获取执行者对象
        Statement stmt = conn.createStatement();
//        写sql语句
        String sql="insert into user(username,psaawrod) values('小乌龟','33')";
//        执行sql
//        返回值:受影响的行数
        int i = stmt.executeUpdate(sql);
//        处理结果集
        System.out.println(i);
//        释放资源
        conn.close();
    }
}
